package com.sxyc.tintin.model;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.amitek.service.bean.response.DeviceInfo;
import com.jfinal.plugin.activerecord.Model;

public class Device extends Model<Device> {

	public static final Device dao = new Device();

	// 使用jFinal 日志
	private com.jfinal.log.Log log = com.jfinal.log.Log.getLog(this.getClass());

	/**
	 * 保存SDK接入平台直接获取的设备信息
	 * 
	 * @param deviceInfo
	 * @return
	 */
	public boolean saveDeviceInfo(DeviceInfo deviceInfo) {
		Device device = null;
		if (deviceInfo != null) {
			device = new Device();
			device.set("DeviceId", deviceInfo.deviceId);
			device.set("ModelStr", deviceInfo.model);
			String[] modelStrs = deviceInfo.model.split("-");
			String serviceId = "";
			if (modelStrs.length > 0) {
				serviceId = modelStrs[0];
			}
			String model = "";
			if (modelStrs.length > 1) {
				model = modelStrs[1];
			}
			String hardVersion = "";
			if (modelStrs.length > 2) {
				hardVersion = modelStrs[2];
			}
			String softVersion = "";
			if (modelStrs.length > 3) {
				softVersion = modelStrs[3];
			}
			device.set("ServiceId", serviceId);
			device.set("Model", model);
			device.set("HardVersion", hardVersion);
			device.set("SoftVersion", softVersion);
			device.set("ICCID", deviceInfo.iccid);
			device.set("MSISDN", deviceInfo.msisdn);
			device.set("IMEI", deviceInfo.imei);
			device.set("IMSI", deviceInfo.imsi);
		}
		return saveDevice(device);
	}

	public boolean saveDevice(Device deviceInfo) {
		boolean isSuccess = false;
		try {
			if (deviceInfo != null) {
				// 保存Device信息
				Device device = getDevice(deviceInfo.get("DeviceId"));
				if (device != null) {
					// 如果以下字段有变更则更新，否则忽略
					if (isModify(device, deviceInfo)) {
						device.set("ModelStr", deviceInfo.get("ModelStr"));
						device.set("ServiceId", deviceInfo.get("ServiceId"));
						device.set("Model", deviceInfo.get("Model"));
						device.set("HardVersion", deviceInfo.get("HardVersion"));
						device.set("SoftVersion", deviceInfo.get("SoftVersion"));
						device.set("ICCID", deviceInfo.get("ICCID"));
						device.set("MSISDN", deviceInfo.get("MSISDN"));
						device.set("IMEI", deviceInfo.get("IMEI"));
						device.set("IMSI", deviceInfo.get("IMSI"));
						device.set("UpdatedBy", "System");
						device.set("UpdatedDate", new Date());
						isSuccess = device.update();
						if (isSuccess) {
							log.info("更新设备信息 [ " + device.toString() + " ] 成功！");
						} else {
							log.info("更新设备信息 [ " + device.toString() + " ] 失败！");
						}
					} else {
						log.info("忽略更新设备信息 [ " + device.toString() + " ] ！");
					}
				} else {
					device = new Device();
					device.set("DeviceId", deviceInfo.get("DeviceId"));
					device.set("ModelStr", deviceInfo.get("ModelStr"));
					device.set("ServiceId", deviceInfo.get("ServiceId"));
					device.set("Model", deviceInfo.get("Model"));
					device.set("HardVersion", deviceInfo.get("HardVersion"));
					device.set("SoftVersion", deviceInfo.get("SoftVersion"));
					device.set("ICCID", deviceInfo.get("ICCID"));
					device.set("MSISDN", deviceInfo.get("MSISDN"));
					device.set("IMEI", deviceInfo.get("IMEI"));
					device.set("IMSI", deviceInfo.get("IMSI"));
					device.set("Remark", "自动导入");
					device.set("Status", 1);
					device.set("CreatedBy", "System");
					device.set("CreatedDate", new Date());
					device.set("UpdatedBy", "System");
					device.set("UpdatedDate", new Date());
					isSuccess = device.save();
					if (isSuccess) {
						log.info("新增设备信息 [ " + device.toString() + " ] 成功！");
					} else {
						log.info("新增设备信息 [ " + device.toString() + " ] 失败！");
					}
				}
			} else {
				log.info("设备信息 为空，忽略保存！");
			}
		} catch (Exception e) {
			log.error("保存设备信息异常，原因：" + e.getMessage());
		}
		return isSuccess;
	}

	public boolean isModify(Device oldDevice, Device newDevice) {
		boolean isModify = false;
		try {
			// log.info("oldDevice："+oldDevice.toString());
			// log.info("newDevice："+newDevice.toString());

			if (oldDevice == null && newDevice != null) {
				isModify = true;
			}
			if (oldDevice != null && newDevice == null) {
				isModify = true;
			}
			if (oldDevice != null && newDevice != null) {
				if (!StringUtils.equals(oldDevice.getStr("ModelStr"), newDevice.getStr("ModelStr"))
						|| !StringUtils.equals(oldDevice.getStr("ServiceId"), newDevice.getStr("ServiceId"))
						|| !StringUtils.equals(oldDevice.getStr("Model"), newDevice.getStr("Model"))
						|| !StringUtils.equals(oldDevice.getStr("HardVersion"), newDevice.getStr("HardVersion"))
						|| !StringUtils.equals(oldDevice.getStr("SoftVersion"), newDevice.getStr("SoftVersion"))
						|| !StringUtils.equals(oldDevice.getStr("ICCID"), newDevice.getStr("ICCID"))
						|| !StringUtils.equals(oldDevice.getStr("MSISDN"), newDevice.getStr("MSISDN"))
						|| !StringUtils.equals(oldDevice.getStr("IMEI"), newDevice.getStr("IMEI"))
						|| !StringUtils.equals(oldDevice.getStr("IMSI"), newDevice.getStr("IMSI"))) {
					isModify = true;
				}
			}

		} catch (Exception e) {
			log.error("判断设备信息是否有更新异常，原因：" + e.getMessage());
		}
		return isModify;
	}

	public Device getDevice(Object object) {
		return Device.dao.findFirst("select * from DeviceInfo where deviceId = ? ", new Object[] { object });
	}

}
